package com.wocai.jrt.orgadmin.asset.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vteba.utils.id.ObjectId;
import com.vteba.web.action.GenericAction;
import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.approve.model.ApproveRoleGroup;
import com.wocai.jrt.orgadmin.approve.model.ApproveRoleGroupMember;
import com.wocai.jrt.orgadmin.approve.service.spi.ApproveRoleGroupMemberService;
import com.wocai.jrt.orgadmin.approve.service.spi.ApproveRoleGroupService;
import com.wocai.jrt.orgadmin.asset.bean.AssetAllocationBean;
import com.wocai.jrt.orgadmin.asset.bean.AssetAllocationHistoryResBean;
import com.wocai.jrt.orgadmin.asset.bean.AssetAllocationReqBean;
import com.wocai.jrt.orgadmin.asset.bean.AssetAllocationResBean;
import com.wocai.jrt.orgadmin.asset.bean.OperaterProductListUtils;
import com.wocai.jrt.orgadmin.asset.bean.OperaterUtils;
import com.wocai.jrt.orgadmin.asset.model.AssetAllocation;
import com.wocai.jrt.orgadmin.asset.model.AssetAllocationDetail;
import com.wocai.jrt.orgadmin.asset.model.AssetAllocationDetailFlowLog;
import com.wocai.jrt.orgadmin.asset.model.AssetAllocationDetailParams;
import com.wocai.jrt.orgadmin.asset.service.spi.AssetAllocationDetailService;
import com.wocai.jrt.orgadmin.asset.service.spi.AssetAllocationService;
import com.wocai.jrt.orgadmin.flow.model.FlowLog;
import com.wocai.jrt.orgadmin.flow.service.spi.FlowInstanceService;
import com.wocai.jrt.orgadmin.flow.service.spi.FlowLogService;
import com.wocai.jrt.orgadmin.product.model.Product;
import com.wocai.jrt.orgadmin.product.model.ProductFundsUse;
import com.wocai.jrt.orgadmin.product.service.spi.ProductService;
import com.wocai.jrt.orgadmin.project.bean.ApproveGroupType;
import com.wocai.jrt.orgadmin.project.bean.ProjectReqBean;
import com.wocai.jrt.orgadmin.project.model.Project;
import com.wocai.jrt.orgadmin.project.service.spi.ProjectService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 资产配置控制器
 * 
 * @author yinlei
 * @date 2016-3-17 17:47:55
 */
@Controller
@RequestMapping("/assetAllocation")
public class AssetAllocationAction extends GenericAction<AssetAllocation> {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(AssetAllocationAction.class);

	@Inject
	private AssetAllocationService assetAllocationServiceImpl;
	@Inject
	private AssetAllocationDetailService assetAllocationDetailServiceImpl;

	@Inject
	private ProductService productServiceImpl;
	@Inject
	private ProjectService projectServiceImpl;
	@Inject
	private FlowInstanceService flowInstanceServiceImpl;
	@Inject
	private FlowLogService flowLogService;
	@Inject
	private ApproveRoleGroupMemberService approveRoleGroupMemberServiceImpl;
	@Inject
	private ApproveRoleGroupService approveRoleGroupServiceImpl;

	private Object model;

	/**
	 * 获得资产配置List列表页面
	 * 
	 * @param model
	 *            参数
	 * @return 资产配置List
	 */
	@RequestMapping("/list")
	public String list() {
		return "asset/list";
	}
	
	/**
	 * 获得资产产品List，Json格式。
	 * 
	 * @param model
	 *            参数
	 * @return 资产配置List
	 */
	@RequestMapping("/productList")
	public String productList(AssetAllocation model) {
		return "asset/productList";
	}
	
	/** 
	* @Title: assetDetail 
	* @Description: 资产配置详情
	* @Time:2016年4月11日 下午4:39:26
	* @param id
	* @param maps 根据 ID 查询资产配置对应的流程日志
	* @return 
	* String    返回类型 
	*/
	@RequestMapping("/assetDetail/{id}")
	public String assetDetail(@PathVariable("id") String id, Map<String, Object> maps) {
		try {
			AssetAllocation assetAllocation = new AssetAllocation();
			assetAllocation = assetAllocationServiceImpl.get(id);
			if (null == assetAllocation) {
				new Exception("assetAllocation is null!");
			}
			Product product = new Product();
			//产品 ID
			product = productServiceImpl.get(assetAllocation.getProductId());
			if (null == product) {
				new Exception("product is null!");
			}
			FlowLog params = new FlowLog();
			params.setEventId(id);
/*			List<FlowLog> listFlowLog = new ArrayList<FlowLog>();
			listFlowLog = flowLogService.queryList(params);*/
			
			List<AssetAllocationDetailFlowLog> listFlowLogs = new ArrayList<AssetAllocationDetailFlowLog>();
			listFlowLogs = assetAllocationDetailServiceImpl.queryFlowLogList(params);//AssetAllocationDetailFlowLog
			
			
			maps.put("listFlowLogList", listFlowLogs); // 返回的流程批复数据
			
			maps.put("assetAllocationID", id);
			//本期资产配置的名称
			maps.put("assetAllocationName", assetAllocation.getName());
			maps.put("productName", product.getBriefName());
			maps.put("productID", product.getId());//productID ,为返回使用
			
		} catch (Exception e) {
			LOGGER.error("query record detail, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "asset/assetDetail";
	}
	
	/** 
	* @Title: assetDetail 
	* @Description: 本期资产配置详情
	* @Time:2016年4月6日 下午4:06:12
	* @param id  资产配置ID，AssetAllocation表 的 ID，也就是 AssetAllocationDetail 表的 asset_allocation_id
	* @param maps
	* @return 
	* String    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/assetDetailList/{id}")
	public Map<String, Object> assetDetailList(@PathVariable("id") String id) {
		 Map<String, Object> maps = new HashMap<String, Object>();
		try {
			List<AssetAllocationDetailParams> aadList = new ArrayList<AssetAllocationDetailParams>();
			AssetAllocationDetail aad = new AssetAllocationDetail();
			aad.setAssetAllocationId(id);
			aadList = assetAllocationDetailServiceImpl.getAllocationDetail(aad);
			int size = assetAllocationDetailServiceImpl.countBy(aad);
			
			maps.put("aaData", aadList); // 返回的数据
			maps.put("iTotalRecords", size); // 总记录数
			maps.put("iTotalDisplayRecords", size); // 显示的记录数
		} catch (Exception e) {
			LOGGER.error("request product query error, msg = [{}]", e.getMessage());
		}
		return maps;
	}
	
	/**
	 * 获得资产配置List列表页面
	 * 
	 * @param model
	 *            参数
	 * @return 资产配置List
	 */
	@ResponseBody
	@RequestMapping("/queryAsset")
	public Map<String, Object> queryAsset(@RequestBody AssetAllocationReqBean model) {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			LOGGER.info("request AssetAllocation query, param = [{}]",
					JSON.toJSONString(model));

			List<AssetAllocationBean> list = assetAllocationServiceImpl.queryAssetAllocationList(model);
			
			List<ApproveGroupType> agList = getApproveGroups(UserUtils.getUserId());
			//设置项目的操作权限
			for (AssetAllocationBean aab : list) {
				for (ApproveGroupType agt : agList) {
					StringBuffer groupTemp = new StringBuffer();
					
					//当前用户是否在 审批角色团队 内
					if(agt.getResult() != null){
						groupTemp.append(agt.getResult());
					}
					
					//approve_role_group Type 参与角色（1投资部，2尽调小组，3项目初审委员会，4项目决策委员会，
					//5法律合规部，6项目最终审批人，7资金运用决策委员会，8资金运用最终审批人）'
					if(agt.getType() != null){
						groupTemp.append(agt.getType()); 
					}
					
					//asset_allocation State (1待审批，2审批中，3终审中，4过终审，5已投资)
					if(String.valueOf(aab.getState()) != null){
						groupTemp.append(String.valueOf(aab.getState()));
					}
					if(!StringUtils.isBlank(groupTemp.toString())){
						//当前操作权限未设置时才添加操作权限
						if(aab.getOperter() == null){
							aab.setOperter(OperaterUtils.get(groupTemp.toString()));	
						}
						
					}
				}
			}
			
			
			
			model.setStart(null);
			
			int size = assetAllocationServiceImpl.countAssetAllocationBy(model);
			map.put("aaData", list); // 返回的数据
			map.put("iTotalRecords", size); // 总记录数
			map.put("iTotalDisplayRecords", size); // 显示的记录数

		} catch (Exception e) {
			LOGGER.error("request AssetAllocation query error, msg = [{}]",
					e.getMessage());
		}
		return map;
	}
	
	/**
	 * 获取该员工所在的审批团队
	 * 有t，代表负责人
	 */
	public List<ApproveGroupType> getApproveGroups(String employeeId){
		List<ApproveGroupType> agList = new ArrayList<ApproveGroupType>();
		try {
			ApproveRoleGroupMember argm = new ApproveRoleGroupMember();
			argm.setEmployeeId(employeeId);
			//获取员工的所有审批团队关系列表
			List<ApproveRoleGroupMember> argmList = approveRoleGroupMemberServiceImpl.pagedList(argm);

			for (ApproveRoleGroupMember approve : argmList) {
				ApproveGroupType agt = new ApproveGroupType();
				ApproveRoleGroup arg = approveRoleGroupServiceImpl.get(approve.getApproveRoleGroupId());
				String a = String.valueOf(arg.getType());
				agt.setType(a);
				if(employeeId.equals(arg.getEmployeeId())){
					agt.setResult("t");
				}
				agList.add(agt);
			}	
		} catch (Exception e) {
			LOGGER.error("getApproveGroups list error, errorMsg=[{}].", e.getMessage());
		}
		return agList;
	}
	
	/** 
	* @Title: querylist 
	* @Description: 获取资金运用产品管理，包含已募集和已使用资金汇总
	* @Time:2016年4月6日 上午11:24:51
	* @return 
	* Map<String,Object>    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/querylist")
	public  Map<String, Object> querylist(@RequestBody Product model){
		Map<String, Object> map = new HashMap<String, Object>();
		List<ProductFundsUse> list = new ArrayList<ProductFundsUse>();

		try {
			LOGGER.info("request product query, param = [{}]", JSON.toJSONString(model));

			String orgId = UserUtils.getUser().getOrgId();
			model.setOrgId(orgId);
			int size = productServiceImpl.countByState(model);
			if (model.getPageSize() == -1) {
				model.setStart(null);
			}

			if (size > 0) {
				list = productServiceImpl.getFundsUse(model);
				LOGGER.info("query product list success, size = [{}]", list.size());

			}

			//判断当前用户所属部门 财富中心和产品部
			List<ApproveGroupType> agList = getApproveGroups(UserUtils.getUserId());
			
			//设置项目的操作权限
			for (ProductFundsUse pfu : list) {
				for (ApproveGroupType agt : agList) {
					StringBuffer groupTemp = new StringBuffer();
					
					//当前用户是否在 审批角色团队 内
					if(agt.getResult() != null){
						groupTemp.append(agt.getResult());
					}
					
					//approve_role_group Type 参与角色（1投资部，2尽调小组，3项目初审委员会，4项目决策委员会，
					//5法律合规部，6项目最终审批人，7资金运用决策委员会，8资金运用最终审批人，9托管部，10财富中心，11产品部'
					if(agt.getType() != null){
						groupTemp.append(agt.getType()); 
					}
					
					//product state '状态 1-募集中，2-募集完成',
					if(String.valueOf(pfu.getState()) != null){
						groupTemp.append(String.valueOf(pfu.getState()));
					}
					//当前操作权限未设置时才添加操作权限
					if(!StringUtils.isBlank(groupTemp.toString())){
						if(pfu.getOperter() == null){
							pfu.setOperter(OperaterProductListUtils.get(groupTemp.toString()));
						}
						
					}
				}
			}
			
			map.put("aaData", list); // 返回的数据
			map.put("iTotalRecords", size); // 总记录数
			map.put("iTotalDisplayRecords", size); // 显示的记录数

		} catch (Exception e) {
			LOGGER.error("request product query error, msg = [{}]", e.getMessage());
		}

		return map;
	}
	
	/**
	 * 跳转到新增页面
	 * add 发起资产配置时判断是否已募集完成，若是，禁止发起资产配置
	 * 
	 * @return 新增页面逻辑视图
	 */
	@RequestMapping("/add/{id}")
	public String add(@PathVariable("id") String id, Map<String, Object> maps) {
		Product product = new Product();
		product = productServiceImpl.get(id);
		if (null == product) {
			return "common/error";
		}
		maps.put("productId", id);
		maps.put("productName", product.getBriefName());

		// TODO 暂时先拉取所有项目 实际是拟投资和已投资为配资满的
		List<Project> projectList = new ArrayList<Project>();
		ProjectReqBean pParm = new ProjectReqBean();
		pParm.setOrgId(UserUtils.getOrgId());
		pParm.setStart(null);
		projectList = projectServiceImpl.pagedList(pParm);
		maps.put("projectList", projectList);
		return "asset/add";
	}
	
	/**
	 * 修改modal信息展示
	 * 
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/edit/{id}")
	public JsonBean edit(@PathVariable("id") String id, Map<String, Object> maps) {
		JsonBean jsonBean = new JsonBean();
		AssetAllocationDetail assetAllocationDetail = new AssetAllocationDetail();
		assetAllocationDetail = assetAllocationDetailServiceImpl.get(id);
		if (null == assetAllocationDetail) {
			jsonBean.setCode(0);
			jsonBean.setMessage("id无效");
			return jsonBean;
		}
		maps.put("assetAllocationDetail", assetAllocationDetail);

		// TODO 暂时先拉取所有项目 实际是拟投资和已投资为配资满的
		List<Project> projectList = new ArrayList<Project>();
		ProjectReqBean pParm = new ProjectReqBean();
		pParm.setOrgId(UserUtils.getOrgId());
		pParm.setStart(null);
		projectList = projectServiceImpl.pagedList(pParm);
		maps.put("projectList", projectList);
		jsonBean.setCode(1);
		jsonBean.setData(maps);
		return jsonBean;
	}
	
	/**
	 * 更新
	 * 
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/update")
	public JsonBean update(@RequestBody AssetAllocation assetAllocation) {
		JsonBean jsonBean = new JsonBean();
		try {
			int result = assetAllocationServiceImpl.updateById(assetAllocation);
			if (1 == result) {
				jsonBean.setCode(1);
				jsonBean.setMessage(SUCCESS);
				LOGGER.info("update assetAllocation success id=[{}]",assetAllocation.getId());
			} else {
				jsonBean.setCode(0);
				jsonBean.setMessage(ERROR);
			}
		} catch (Exception e) {
			jsonBean.setCode(0);
			jsonBean.setMessage(e.getMessage());
		}
		return jsonBean;
	}
	
	
	/**
	 * 个人表决
	 * 
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/vote")
	public JsonBean vote(@RequestBody FlowLog flowLog) {
		JsonBean jsonBean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(flowLog.getEventId(),flowLog.getResult().toString(),flowLog.getSuggestion())) {
				jsonBean.setCode(0);
				jsonBean.setMessage("参数不能为空");
				return jsonBean;
			}
			jsonBean = assetAllocationServiceImpl.voteHandle(flowLog);
		} catch (Exception e) {
			jsonBean.setCode(0);
			jsonBean.setMessage(e.getMessage());
		}
		return jsonBean;
	}
	
	/** 
	* @Title: approve 
	* @Description: 决策委员会负责人
	* @Time:2016年4月14日 上午9:36:21
	* @param flowLog
	* @return 
	* JsonBean    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/approve")
	public JsonBean approve(@RequestBody FlowLog flowLog) {
		JsonBean jsonBean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(flowLog.getEventId(),flowLog.getResult().toString(),flowLog.getSuggestion())) {
				jsonBean.setCode(0);
				jsonBean.setMessage("参数不能为空");
				return jsonBean;
			}
			jsonBean = assetAllocationServiceImpl.approveHandle(flowLog);
		} catch (Exception e) {
			jsonBean.setCode(0);
			jsonBean.setMessage(e.getMessage());
		}
		return jsonBean;
	}
	
	/** 
	* @Title: approveFinal 
	* @Description: 终身人审批
	* @Time:2016年4月14日 下午4:14:09
	* @param flowLog
	* @return 
	* JsonBean    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/approveFinal")
	public JsonBean approveFinal(@RequestBody FlowLog flowLog) {
		JsonBean jsonBean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(flowLog.getEventId(),flowLog.getResult().toString(),flowLog.getSuggestion())) {
				jsonBean.setCode(0);
				jsonBean.setMessage("参数不能为空");
				return jsonBean;
			}
			jsonBean = assetAllocationServiceImpl.approveFinalHandle(flowLog);
		} catch (Exception e) {
			jsonBean.setCode(0);
			jsonBean.setMessage(e.getMessage());
		}
		return jsonBean;
	}
	
	/** 
	* @Title: submitAudit 
	* @Description: 资产配置提交审核，ADD 判断对应的资产配置明细 是否有数据
	* @Time:2016年4月11日 下午4:05:29
	* @param id  AssetAllocation_ID
	* @return 
	* JsonBean    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/submitAudit")
	public JsonBean submitAudit(String id){
		JsonBean jsonBean = new JsonBean();
		AssetAllocationDetail  params = new AssetAllocationDetail();
		params.setAssetAllocationId(id);
		if (StringUtils.isBlank(id) || (0 == assetAllocationDetailServiceImpl.countBy(params))) {
			jsonBean.setCode(0);
			jsonBean.setMessage("请先添加资产配置");
			return jsonBean;
		}

		jsonBean = assetAllocationServiceImpl.submitAudit(id);
		return jsonBean;
	}

	/**
	 * 执行实际的新增操作
	 * 
	 * @param model
	 *            要新增的数据
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/doAdd")
	public JsonBean doAdd(@RequestBody AssetAllocation model) {
		JsonBean bean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(model.getName(), model.getProductId())) {
				bean.setCode(0);
				bean.setMessage("参数不能为空");
				return bean;
			}
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("add assetAllocation query, param = [{}]",
						JSON.toJSONString(model));
			}
			
			bean = assetAllocationServiceImpl.saveAssetAllocation(model);
		} catch (Exception e) {
			LOGGER.error("save record error, errorMsg=[{}].", e.getMessage());
			bean.setCode(0);
			bean.setMessage(ERROR);
		}
		return bean;
	}

	/**
	 * 执行实际的新增操作
	 * 
	 * @param model
	 *            要新增的数据
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/doEditDetail")
	public JsonBean doEditDetail(@RequestBody AssetAllocationDetail model) {
		JsonBean bean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(model.getId(),
					model.getProjectId())) {
				bean.setCode(0);
				bean.setMessage("参数不能为空");
				return bean;
			}
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("add assetAllocationDetail query, param = [{}]",
						JSON.toJSONString(model));
			}

			int result = assetAllocationDetailServiceImpl.updateById(model);
			if (result == 1) {
				bean.setMessage(SUCCESS);
				bean.setCode(1);
				LOGGER.info("edit record success.");
			} else {
				bean.setCode(0);
				bean.setMessage(ERROR);
				LOGGER.error("edit record error.");
			}
		} catch (Exception e) {
			LOGGER.error("edit record error, errorMsg=[{}].", e.getMessage());
			bean.setCode(0);
			bean.setMessage(ERROR);
		}
		return bean;
	}
	
	/**
	 * 执行实际的新增操作
	 * 
	 * @param model
	 *            要新增的数据
	 * @return 新增页面逻辑视图
	 */
	@ResponseBody
	@RequestMapping("/doAddDetail")
	public JsonBean doAddDetail(@RequestBody AssetAllocationDetail model) {
		JsonBean bean = new JsonBean();
		try {
			if (StringUtils.isAnyBlank(model.getAssetAllocationId(),
					model.getProjectId())) {
				bean.setCode(0);
				bean.setMessage("参数不能为空");
				return bean;
			}
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("add assetAllocationDetail query, param = [{}]",
						JSON.toJSONString(model));
			}
			
			model.setId(ObjectId.get().toString());
			model.setOrgId(UserUtils.getOrgId());
			model.setCreateTime(new Date());

			int result = assetAllocationDetailServiceImpl.save(model);
			if (result == 1) {
				bean.setMessage(SUCCESS);
				bean.setCode(1);
				bean.setData(model);
				LOGGER.info("save record success.");
			} else {
				bean.setCode(0);
				bean.setMessage(ERROR);
				LOGGER.error("save record error.");
			}
		} catch (Exception e) {
			LOGGER.error("save record error, errorMsg=[{}].", e.getMessage());
			bean.setCode(0);
			bean.setMessage(ERROR);
		}
		return bean;
	}

	/**
	 * 新增资产配置页面发起的查询明细列表
	 * 
	 * @param model
	 *            参数
	 * @return 意见反馈List
	 */
	@ResponseBody
	@RequestMapping("/query")
	public Map<String, Object> query(@RequestBody AssetAllocationReqBean model) {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			LOGGER.info("request AssetAllocation query, param = [{}]",
					JSON.toJSONString(model));

			int size = assetAllocationServiceImpl
					.countAssetAllocationResBeanList(model);

			List<AssetAllocationResBean> list = new ArrayList<AssetAllocationResBean>();

			if (size > 0) {
				list = assetAllocationServiceImpl
						.getAssetAllocationResBeanList(model);
				LOGGER.info("query product list success, size = [{}]",
						list.size());

			}

			map.put("aaData", list); // 返回的数据
			map.put("iTotalRecords", size); // 总记录数
			map.put("iTotalDisplayRecords", size); // 显示的记录数

		} catch (Exception e) {
			LOGGER.error("request product query error, msg = [{}]",
					e.getMessage());
		}

		return map;
	}

	/**
	 * 查看资产配置详情页。
	 * 
	 * @param model
	 *            查询参数，携带ID
	 * @return 资产配置详情页
	 */
	@RequestMapping("/history/{id}")
	public String detail(@PathVariable("id") String id, Map<String, Object> maps) {
		Product model = new Product();
		try {
			model = productServiceImpl.get(id);
			if (null == model) {
				LOGGER.error("id错误");
				return "common/error";
			}
			maps.put("product", model);// 将model放入视图中，供页面视图使用
		} catch (Exception e) {
			LOGGER.error("query record detail, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "asset/history";
	}
	
	/**
	 * 历史资产配置列表
	 * 
	 * @param model
	 *            参数
	 * @return 意见反馈List
	 */
	@ResponseBody
	@RequestMapping("/queryHistory")
	public Map<String, Object> queryHistory(@RequestBody Product product) {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			LOGGER.info("request AssetAllocation query, param = [{}]",
					JSON.toJSONString(model));

			int size = assetAllocationServiceImpl.countAssetAllocationHistory(product.getId());

			List<AssetAllocationHistoryResBean> list = new ArrayList<AssetAllocationHistoryResBean>();

			if (size > 0) {
				list = assetAllocationServiceImpl.getAssetAllocationHistory(product.getId());
				LOGGER.info("query product list success, size = [{}]",
						list.size());
			}

			map.put("aaData", list); // 返回的数据
			map.put("iTotalRecords", size); // 总记录数
			map.put("iTotalDisplayRecords", size); // 显示的记录数

		} catch (Exception e) {
			LOGGER.error("request product query error, msg = [{}]",
					e.getMessage());
		}

		return map;
	}

	/**
	 * 删除资产配置信息
	 * 
	 * @param id
	 *            要删除的资产配置ID
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public JsonBean delete(String id) {
		JsonBean bean = new JsonBean();
		try {
			int result = assetAllocationDetailServiceImpl.deleteById(id);
			if (result == 1) {
				bean.setMessage(SUCCESS);
				bean.setCode(1);
				LOGGER.info("delete record success, id=[{}].", id);
			} else {
				bean.setMessage(ERROR);
				LOGGER.error("delete record error.");
			}
		} catch (Exception e) {
			LOGGER.error("delete record error, id=[{}], errorMsg=[{}].", id,
					e.getMessage());
			bean.setMessage(ERROR);
		}
		return bean;
	}
	
	/** 
	* @Title: raiseFinish 
	* @Description: 确认募集完成
	* @Time:2016年4月8日 下午2:40:23
	* @param id
	* @return 
	* JsonBean    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/raiseFinish")
	public JsonBean raiseFinish(String id) {
		JsonBean bean = new JsonBean();
		try {
			Product product = productServiceImpl.get(id);
			if(null == product){
				bean.setMessage(ERROR);
				bean.setCode(0);
				LOGGER.info("Product is not find, id=[{}].", id);
				return bean;
			}
			
			// product.status '状态 0-即将上线，1-募集中，2-募集完成',
			if(1 != product.getState()){
				bean.setMessage(ERROR);
				bean.setCode(0);
				LOGGER.info("Product status <> 1, id=[{}].", id);
				return bean;
			} 
			
			product.setState(2);
			if(0 == productServiceImpl.updateById(product)){
				bean.setMessage(ERROR);
				bean.setCode(0);
				LOGGER.info("Product status update false, id=[{}].", id);
				return bean;
			}
			
			bean.setMessage(SUCCESS);
			bean.setCode(1);
			LOGGER.info("Product status update success, id=[{}].", id);
			
		} catch (Exception e) {
			LOGGER.error("Update Product status error, id=[{}], errorMsg=[{}].", id,
					e.getMessage());
			bean.setMessage(ERROR);
			bean.setCode(0);
		}
		return bean;
	}
	
	/** 
	* @Title: queryApprove 
	* @Description: 决策委员会负责人审批，查看个人表决意见 
	* @Time:2016年4月12日 下午2:11:34
	* @param productID
	* @return 
	* Map<String,Object>    返回类型 
	*/
	@ResponseBody
	@RequestMapping("/queryApprove/{id}")
	public Map<String, Object> queryApprove(@PathVariable("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			FlowLog params = new FlowLog();
			params.setEventId(id);
			params.setStepName("资产运用决策委员会审批");//资产运用决策委员会审批
			int size = flowLogService.countBy(params);

			List<FlowLog> list = new ArrayList<FlowLog>();
			if (size > 0) {
				list = flowLogService.queryList(params);
				LOGGER.info("query FlowLog list success, size = [{}]",
						list.size());
			}

			map.put("aaData", list); // 返回的数据
			map.put("iTotalRecords", size); // 总记录数
			map.put("iTotalDisplayRecords", size); // 显示的记录数

		} catch (Exception e) {
			LOGGER.error("request FlowLog query error, msg = [{}]",
					e.getMessage());
		}

		return map;
	}
}
